const path = require('path');
const utils = require('../component/utils');
const _ = require('lodash');
const glob = require('glob-all');

module.exports = function (config, {subProjectName}) {
    const scriptFiles = glob.sync(`src/${subProjectName}/pages/*/*.js`, {
        cwd: path.resolve(__dirname, '..')
    });

    const entry = _.chain(scriptFiles).map(function (filename) {
        const baseName = path.basename(filename, '.js');
        return [baseName, [`./${filename}`]];
    }).fromPairs().value();

    return {
        entry: entry,
        resolve: {
            extensions: ['.js', '.vue', '.json'],
            modules: [
                path.resolve(__dirname, '../src'),
                path.resolve(__dirname, '../node_modules')
            ],
            alias: {
                //-------以下不能改变--------
                'vue$': 'vue/dist/vue.esm.js',
                '$PEDESTAL': path.resolve(__dirname, '../src/pedestal/libs'),
                '$USER': path.resolve(__dirname, '../src/user/libs'),
                //---------------
            }
        },
        module: {
            rules: [
                {
                    test: /\.vue$/,
                    loader: 'vue-loader',
                    exclude: new RegExp('/node_modules/'),
                    options: {
                        loaders: utils.cssLoaders({
                            sourceMap: config.sourceMap,
                            extract: config.extract
                        }),
                        postcss: [
                            require('autoprefixer')({
                                browsers: ['last 2 versions']
                            })
                        ]
                    }
                },
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: new RegExp('/node_modules/'),
                    include: [path.resolve(__dirname, '../src'), path.resolve(__dirname, '../test')]
                },
                {
                    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                    loader: 'url-loader',
                    query: {
                        limit: 10000,
                        name: utils.assetsPath('img/[name].[hash:7].[ext]', config.assetsSubDirectory)
                    }
                },
                {
                    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                    loader: 'url-loader',
                    query: {
                        limit: 10000,
                        name: utils.assetsPath('fonts/[name].[hash:7].[ext]', config.assetsSubDirectory)
                    }
                },
                {
                    test: /\.scss$/,
                    loader: 'style!css!sass'
                }
            ]
        }
    };
};
